﻿// =====================================================================
// <copyright file="ExpressionExtensions.cs" author="David R. Guindo">
// 	Copyright (C) 2013. All rights reserved.
// </copyright>
// =====================================================================
// This code is released under the terms of the Apache 2.0 license, 
// http://apache.org/licenses/LICENSE-2.0.html
// =====================================================================

namespace System.Linq.Expressions
{
    using System.Diagnostics.CodeAnalysis;

    public static class ExpressionExtensions
    {
        [SuppressMessage("Microsoft.Design", "CA1011")]
        [SuppressMessage("Microsoft.Design", "CA1006")]
        public static string PropertyName(Expression<Func<object>> exp)
        {
            var body = exp.Body as MemberExpression ?? (exp.Body as UnaryExpression).Operand as MemberExpression;
            return body.Member.Name;
        }

        public static string PropertyName<T, P>(Expression<Func<T, P>> action)
        {
            var expression = (MemberExpression) action.Body;
            return expression.Member.Name;
        }
    }
}